/*==============================================================*/
/* DBMS name:      Microsoft SQL Server 2008                    */
/* Created on:     11/26/2014 8:05:29 PM                        */
/*==============================================================*/


if exists (select 1
   from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('CAUHOI') and o.name = 'FK_CAUHOI_THUOC_CHUDE')
alter table CAUHOI
   drop constraint FK_CAUHOI_THUOC_CHUDE
go

if exists (select 1
   from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('LICHSU') and o.name = 'FK_LICHSU_CO_CHUDE')
alter table LICHSU
   drop constraint FK_LICHSU_CO_CHUDE
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('CAUHOI')
            and   name  = 'THUOC_FK'
            and   indid > 0
            and   indid < 255)
   drop index CAUHOI.THUOC_FK
go

if exists (select 1
            from  sysobjects
           where  id = object_id('CAUHOI')
            and   type = 'U')
   drop table CAUHOI
go

if exists (select 1
            from  sysobjects
           where  id = object_id('CHUDE')
            and   type = 'U')
   drop table CHUDE
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('LICHSU')
            and   name  = 'CO_FK'
            and   indid > 0
            and   indid < 255)
   drop index LICHSU.CO_FK
go

if exists (select 1
            from  sysobjects
           where  id = object_id('LICHSU')
            and   type = 'U')
   drop table LICHSU
go

/*==============================================================*/
/* Table: CAUHOI                                                */
/*==============================================================*/
create table CAUHOI (
   CH_MA                int                  not null,
   CD_MA                char(4)              null,
   CH_NOIDUNG           nvarchar(1000)        null,
   CH_A                 nvarchar(300)         null,
   CH_B                 nvarchar(300)         null,
   CH_C                 nvarchar(300)         null,
   CH_D                 nvarchar(300)         null,
   CH_DAPAN             char(1)              null,
   constraint PK_CAUHOI primary key nonclustered (CH_MA)
)
go

/*==============================================================*/
/* Index: THUOC_FK                                              */
/*==============================================================*/
create index THUOC_FK on CAUHOI (
CD_MA ASC
)
go

/*==============================================================*/
/* Table: CHUDE                                                 */
/*==============================================================*/
create table CHUDE (
   CD_MA                char(4)             not null,
   CD_TEN               nvarchar(100)             null,
   constraint PK_CHUDE primary key nonclustered (CD_MA)
)
go

/*==============================================================*/
/* Table: LICHSU                                                */
/*==============================================================*/
create table LICHSU (
   LS_STT               int                  not null,
   CD_MA                char(4)             null,
   LS_DIEM              float(2)             null,
   LS_TONGSOCAU         smallint             null,
   LS_SOCAUDUNG         smallint             null,
   LS_NGAY              datetime             null,
   constraint PK_LICHSU primary key nonclustered (LS_STT)
)
go

/*==============================================================*/
/* Index: CO_FK                                                 */
/*==============================================================*/
create index CO_FK on LICHSU (
CD_MA ASC
)
go

alter table CAUHOI
   add constraint FK_CAUHOI_THUOC_CHUDE foreign key (CD_MA)
      references CHUDE (CD_MA)
go

alter table LICHSU
   add constraint FK_LICHSU_CO_CHUDE foreign key (CD_MA)
      references CHUDE (CD_MA)
go

